<!DOCTYPE html><html lang="zh-CN"><head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>duration() | Moment.js &#x6587;&#x6863;</title>
  <link rel="stylesheet" href="static/css/style.css">
  <link rel="shortcut icon" href="http://momentjs.cn/static/img/moment-favicon.png">
  <link rel="stylesheet" href="static/css/atom-one-light.min.css">
</head>
<body class="is-moment">
  <div class="hero hero-oneline">
    <div class="hero-centered">
      <h1>duration()</h1>
    </div>
  </div>
  <div id="api-section-__FILENAME__">
    <div id="content" class="clearfix">
  
      <div id="column1" data-id="__ID__" class="interior">
        <header>
          <!-- <h1>duration()</h1> -->
          <div id="gtoc">
            <ul>
              <!-- <li>v</li> -->
              <li>
                <a href="index.html#/durations/creating/" name="toc">&#x8FD4;&#x56DE;&#x4E0A;&#x5C42;&#x6587;&#x6863;</a>
              </li>
              <li style="border: none;">
                <a href="javascript:window.open('https://github.com/momentjscn/moment-docs-cn/edit/master/moment/08-durations/01-creating.md');" rel="nofollow" target="_blank">&#x63D0;&#x4EA4;&#x4FEE;&#x6539;</a>
              </li>
              <!-- <li>
                <a href="/search__SEARCH_PARAM__" name="toc">搜索</a>
              </li> -->
            </ul>
          </div>
          <hr>
        </header>

        <div id="biz_item"></div>
  
        <div id="apicontent">
          <div id="content_left">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment.duration(Number, String);
moment.duration(Number);
moment.duration(Object);
moment.duration(String);
</code></pre>

							</div>
<p>&#x8981;&#x521B;&#x5EFA;&#x65F6;&#x957F;&#xFF0C;&#x5219;&#x8C03;&#x7528; <code>moment.duration()</code>&#xFF0C;&#x5E76;&#x4EE5;&#x6BEB;&#x79D2;&#x4E3A;&#x5355;&#x4F4D;&#x3002;</p>
<pre><code class="language-javascript">moment.duration(100); // 100 &#x6BEB;&#x79D2;
</code></pre>
<p>&#x5982;&#x679C;&#x8981;&#x4F7F;&#x7528;&#x6BEB;&#x79D2;&#x4EE5;&#x5916;&#x7684;&#x5176;&#x4ED6;&#x5EA6;&#x91CF;&#x5355;&#x4F4D;&#x6765;&#x521B;&#x5EFA; moment&#xFF0C;&#x5219;&#x4E5F;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x5EA6;&#x91CF;&#x5355;&#x4F4D;&#x3002;</p>
<pre><code class="language-javascript">moment.duration(2, &apos;seconds&apos;);
moment.duration(2, &apos;minutes&apos;);
moment.duration(2, &apos;hours&apos;);
moment.duration(2, &apos;days&apos;);
moment.duration(2, &apos;weeks&apos;);
moment.duration(2, &apos;months&apos;);
moment.duration(2, &apos;years&apos;);
</code></pre>
<p>&#x540C;&#x6837;&#xFF0C;<code>moment#add</code> &#x548C; <code>moment#subtract</code>&#x7684;&#x7B80;&#x5199;&#x5728;&#x8FD9;&#x91CC;&#x4E5F;&#x9002;&#x7528;&#x3002;</p>
<table class="table table-striped table-bordered">
  <tbody>
    <tr>
      <th>&#x952E;</th>
      <th>&#x7B80;&#x5199;</th>
    </tr>
    <tr>
      <td>years</td>
      <td>y</td>
    </tr>
    <tr>
      <td>months</td>
      <td>M</td>
    </tr>
    <tr>
      <td>weeks</td>
      <td>w</td>
    </tr>
    <tr>
      <td>days</td>
      <td>d</td>
    </tr>
    <tr>
      <td>hours</td>
      <td>h</td>
    </tr>
    <tr>
      <td>minutes</td>
      <td>m</td>
    </tr>
    <tr>
      <td>seconds</td>
      <td>s</td>
    </tr>
    <tr>
      <td>milliseconds</td>
      <td>ms</td>
    </tr>
  </tbody>
</table>
<p>&#x4E0E; <code>moment#add</code> &#x76F8;&#x4F3C;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x591A;&#x4E2A;&#x4E0D;&#x540C;&#x7684;&#x5EA6;&#x91CF;&#x5355;&#x4F4D;&#xFF0C;&#x5219;&#x53EF;&#x4EE5;&#x4F20;&#x5165;&#x503C;&#x7684;&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="language-javascript">moment.duration({
    seconds: 2,
    minutes: 2,
    hours: 2,
    days: 2,
    weeks: 2,
    months: 2,
    years: 2
});
</code></pre>
<p>&#x4ECE; <strong>2.1.0</strong> &#x5F00;&#x59CB;&#xFF0C;moment &#x652F;&#x6301;&#x89E3;&#x6790; ASP.NET &#x98CE;&#x683C;&#x7684;&#x65F6;&#x95F4;&#x8DE8;&#x5EA6;&#x3002;
&#x652F;&#x6301;&#x4EE5;&#x4E0B;&#x683C;&#x5F0F;&#x3002;</p>
<p>&#x683C;&#x5F0F;&#x662F;&#x4EE5;&#x5192;&#x53F7;&#x5206;&#x9694;&#x7684;&#x5C0F;&#x65F6;&#x3001;&#x5206;&#x949F;&#x3001;&#x79D2;&#x949F;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x4F8B;&#x5982; <code>23:59:59</code>&#x3002;
&#x5929;&#x6570;&#x53EF;&#x4EE5;&#x52A0;&#x4E0A;&#x70B9;&#x5206;&#x9694;&#x7B26;&#xFF0C;&#x5982; <code>7.23:59:59&#x3002; &#x8FD8;&#x652F;&#x6301;&#x90E8;&#x5206;&#x79D2;&#x6570;&#x5982;</code>23:59:59.999`&#x3002;</p>
<pre><code class="language-javascript">moment.duration(&apos;23:59:59&apos;);
moment.duration(&apos;23:59:59.999&apos;);
moment.duration(&apos;7.23:59:59.999&apos;);
moment.duration(&apos;23:59&apos;); // &#x65B0;&#x589E;&#x4E8E; 2.3.0
</code></pre>
<p>&#x4ECE; <strong>2.3.0</strong> &#x5F00;&#x59CB;&#xFF0C;moment &#x8FD8;&#x652F;&#x6301;&#x89E3;&#x6790; <a href="javascript:window.open('http://nodejs.cn/s/d4dKhv');" target="_blank" rel="nofollow">ISO 8601</a> &#x65F6;&#x957F;&#x3002;</p>
<pre><code class="language-javascript">moment.duration(&apos;P1Y2M3DT4H5M6S&apos;);
moment.duration(&apos;P1M&apos;);
</code></pre>
<p>&#x4ECE; <strong>2.11.0</strong> &#x5F00;&#x59CB;&#xFF0C;&#x652F;&#x6301;&#x65F6;&#x957F;&#x7684;&#x683C;&#x5F0F;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5176;&#x4E2D;&#x5929;&#x6570;&#x548C;&#x5269;&#x4E0B;&#x7684;&#x65F6;&#x95F4;&#x4E4B;&#x95F4;&#x6709;&#x7A7A;&#x683C;&#x3002;</p>
<pre><code class="language-javascript">moment.duration(&apos;7 23:59:59.999&apos;);
</code></pre>
<p>&#x4ECE; <strong>2.13.0</strong> &#x5F00;&#x59CB;&#xFF0C;&#x89E3;&#x6790;&#x65F6;&#x957F;&#x65F6;&#x652F;&#x6301;&#x6DF7;&#x5408;&#x7684;&#x8D1F;&#x53F7;&#x548C;&#x6B63;&#x53F7;&#x3002;</p>
<pre><code class="language-javascript">moment.duration(&apos;PT-6H3M&apos;)
</code></pre>
<p>&#x4ECE; <strong>2.18.0</strong> &#x5F00;&#x59CB;&#xFF0C;&#x652F;&#x6301;&#x65E0;&#x6548;&#x7684;&#x65F6;&#x957F;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x65E0;&#x6548;&#x7684;&#x65F6;&#x523B;&#x3002;
&#x8981;&#x521B;&#x5EFA;&#x65E0;&#x6548;&#x7684;&#x65F6;&#x957F;&#xFF0C;&#x53EF;&#x4EE5;&#x4E3A;&#x5355;&#x4F4D;&#x7684;&#x503C;&#x4F20;&#x5165; <code>NaN</code>&#x3002;</p>
<p>&#x5728;&#x5373;&#x5C06;&#x53D1;&#x5E03;&#x7684;&#x7248;&#x672C;&#x4E2D;&#xFF0C;&#x9884;&#x671F;&#x65E0;&#x6548;&#x7684;&#x65F6;&#x957F;&#x53EF;&#x4EE5;&#x8986;&#x76D6;&#x66F4;&#x591A;&#x60C5;&#x51B5;&#xFF08;&#x4F8B;&#x5982;&#x5355;&#x4F4D;&#x7684;&#x7A7A;&#x503C;&#xFF09;&#x3002;</p>
<pre><code class="language-javascript">moment.duration(NaN);
moment.duration(NaN, &apos;days&apos;);
moment.duration.invalid();
</code></pre>
					</div>
          <div id="content_right">
							<div class="docs-method-signature">
								<pre><code class="language-js">moment.duration(Number, String);
moment.duration(Number);
moment.duration(Object);
moment.duration(String);
</code></pre>

							</div>
<p>To create a duration, call <code>moment.duration()</code> with the length of time in milliseconds.</p>
<pre><code class="language-javascript">moment.duration(100); // 100 milliseconds
</code></pre>
<p>If you want to create a moment with a unit of measurement other than milliseconds, you can pass the unit of measurement as well.</p>
<pre><code class="language-javascript">moment.duration(2, &apos;seconds&apos;);
moment.duration(2, &apos;minutes&apos;);
moment.duration(2, &apos;hours&apos;);
moment.duration(2, &apos;days&apos;);
moment.duration(2, &apos;weeks&apos;);
moment.duration(2, &apos;months&apos;);
moment.duration(2, &apos;years&apos;);
</code></pre>
<p>The same shorthand for <code>moment#add</code> and <code>moment#subtract</code> works here as well.</p>
<table class="table table-striped table-bordered">
  <tbody>
    <tr>
      <th>Key</th>
      <th>Shorthand</th>
    </tr>
    <tr>
      <td>years</td>
      <td>y</td>
    </tr>
    <tr>
      <td>months</td>
      <td>M</td>
    </tr>
    <tr>
      <td>weeks</td>
      <td>w</td>
    </tr>
    <tr>
      <td>days</td>
      <td>d</td>
    </tr>
    <tr>
      <td>hours</td>
      <td>h</td>
    </tr>
    <tr>
      <td>minutes</td>
      <td>m</td>
    </tr>
    <tr>
      <td>seconds</td>
      <td>s</td>
    </tr>
    <tr>
      <td>milliseconds</td>
      <td>ms</td>
    </tr>
  </tbody>
</table>
<p>Much like <code>moment#add</code>, you can pass an object of values if you need multiple different units of measurement.</p>
<pre><code class="language-javascript">moment.duration({
    seconds: 2,
    minutes: 2,
    hours: 2,
    days: 2,
    weeks: 2,
    months: 2,
    years: 2
});
</code></pre>
<p>As of <strong>2.1.0</strong>, moment supports parsing ASP.NET style time spans. The following formats are supported.</p>
<p>The format is an hour, minute, second string separated by colons like <code>23:59:59</code>. The number of days can be prefixed with a dot separator like so <code>7.23:59:59</code>. Partial seconds are supported as well <code>23:59:59.999</code>.</p>
<pre><code class="language-javascript">moment.duration(&apos;23:59:59&apos;);
moment.duration(&apos;23:59:59.999&apos;);
moment.duration(&apos;7.23:59:59.999&apos;);
moment.duration(&apos;23:59&apos;); // added in 2.3.0
</code></pre>
<p>As of <strong>2.3.0</strong>, moment also supports parsing <a href="javascript:window.open('https://en.wikipedia.org/wiki/ISO_8601#Time_intervals');" target="_blank" rel="nofollow">ISO 8601</a> durations.</p>
<pre><code class="language-javascript">moment.duration(&apos;P1Y2M3DT4H5M6S&apos;);
moment.duration(&apos;P1M&apos;);
</code></pre>
<p>As of <strong>2.11.0</strong>, duration format strings with a space between days and rest
is supported.</p>
<pre><code class="language-javascript">moment.duration(&apos;7 23:59:59.999&apos;);
</code></pre>
<p>As of <strong>2.13.0</strong>, mixed negative and positive signs are supported when parsing durations.</p>
<pre><code class="language-javascript">moment.duration(&apos;PT-6H3M&apos;)
</code></pre>
<p>As of <strong>2.18.0</strong>, invalid durations are supported, similarly to invalid
moments. To create an invalid duration you can pass <code>NaN</code> for a value of
a unit.</p>
<p>In upcoming releases expect invalid durations to cover more cases (like
null values for units).</p>
<pre><code class="language-javascript">moment.duration(NaN);
moment.duration(NaN, &apos;days&apos;);
moment.duration.invalid();
</code></pre>
					</div>
          <div></div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="static/js/highlight.min.js"></script>
  <script src="static/js/javascript.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  <script>var _hmt = _hmt || []; (function () { var hm = document.createElement("script"); hm.src = "//hm.baidu.com/hm.js?7a7f78c805fbacc7e075d34f153031aa"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();</script>

  <style>
    .hero-centered {
      margin: 0;
          margin-left: 10px;
    }
#gtoc ul {
  list-style: none;
  margin-left: 0;
  line-height: 1.5rem;
  padding: 0;
}
#gtoc>ul>li {
  display: inline;
  border-right: 1px solid #000;
  margin-right: .4rem;
  padding-right: .4rem;
}
#gtoc>ul>li>a {
  color: #61b2a7;
}
#content_left {
  width: 49%;float: left;border-right: 1px solid #eee;    padding-right: 2%;
}
#content_right {
float: left;width: 48%;margin-left: 3%;
}
#column1 {
  padding: 0 2rem;
}
  </style>


</body></html>